import goto
import label as label
from appium import webdriver
from selenium.webdriver.support import expected_conditions as EC
import time
from goto import with_goto
import pandas

# 设置终端参数项
from appium.webdriver.common.touch_action import TouchAction
from selenium.webdriver.support.wait import WebDriverWait

# 当前次数
count = 0
# for循环内的计数器
count_i = 0
# 本页都是vip文章 需要向下滑动
is_all_vip = False
# 向上拉几次
pull_count = 40

desired_caps = {
    'platformName': 'Android',
    'platformVersion': '9',
    'deviceName': '127.0.0.1:62001',
    'appPackage': 'lnwlztb.study',
    'appActivity': '.activity.SplashActivity',
    'noReset': True
}

# 发送指令给到appium server
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
# 获取屏幕的width和height

size = driver.get_window_size()
width = size['width']
height = size['height']
isAllVipFlag = True


# 向上滑
def swipeUp(driver, t=1000, n=1):
    global width
    global height
    """向上屏幕滑动"""
    x1 = size["width"] * 0.5  # x坐标
    y1 = size["height"] * 0.5  # 起点 y坐标
    y2 = size["height"] * 0.10  # 终点 y 坐标
    for i in range(n):
        time.sleep(1)
        driver.swipe(x1, y1, x1, y2, t)


# 向下滑
def swipeDown(driver, t=1000, n=2):
    """向下屏幕滑动"""
    x1 = size["width"] * 0.5  # x1 坐标
    y1 = size["height"] * 0.25  # 起点y1坐标
    y2 = size["height"] * 0.75  # 终点y2坐标
    for i in range(n):
        time.sleep(1)
        driver.swipe(x1, y1, x1, y2, t)


# 向左滑
def swipeLeft(driver, t=500, n=1):
    """向左屏幕滑动"""
    x1 = size["width"] * 0.75  # 起点x1坐标
    y1 = size["height"] * 0.5  # y1 坐标
    x2 = size["width"] * 0.25  # 终点x2坐标
    for i in range(n):
        time.sleep(1)
        driver.swipe(x1, y1, x2, y1, t)


# 向右滑
def swipeRight(driver, t=500, n=1):
    """向右屏幕滑动"""
    x1 = size["width"] * 0.25  # 起点x1坐标
    y1 = size["height"] * 0.5  # y1坐标
    x2 = size["width"] * 0.75  # 终点x2坐标
    for i in range(n):
        time.sleep(1)
        driver.swipe(x1, y1, x2, y1, t)


# 等待广告页时间
# driver.implicitly_wait(11)
time.sleep(11)
# driver.find_element_by_id("lnwlztb.study:id/tv_ad_skip").click()
# swipeUp(driver, 50, 1)
# swipeDown(driver, 80, 1)
# @with_goto
# def rolling():
# global count
# global count_i
# global is_all_vip
# global pull_count
for i in range(1000):
    # 点击学习轨迹
    time.sleep(3)
    if (count == 0):
        time.sleep(1)
        swipeUp(driver, 1000, 1)
        count += 1
    time.sleep(2)
    driver.find_element_by_id("lnwlztb.study:id/tv_reading_more").click()
    time.sleep(4)
    # 点击未学习列表的第n项
    # driver.find_element_by_id("lnwlztb.study:id/icon_wz").click()
    # driver.find_element_by_xpath(
    #     "//android.widget.ImageView[@resource-id='lnwlztb.study:id/icon_wz']").click()
    # label.begin
    time.sleep(1)
    swipeUp(driver, 2000, pull_count)
    time.sleep(15)
    # for i in range(8):
    # count_i = i
    count = 0
    small_count = 1  # 当前页面的每一项计数器
    while True:
        if(count == 0 or count % 6 == 0):
            time.sleep(3)
        ele = driver.find_elements_by_xpath(f"//*[@resource-id='lnwlztb.study:id/list']/android.widget.RelativeLayout[{small_count}]/android.widget.RelativeLayout[1]/android.widget.LinearLayout[1]/android.widget.ImageView[1]")
        # vip = ("xpath",
        #        f"//*[@resource-id='lnwlztb.study:id/list']/android.widget.RelativeLayout[{small_count}]/android.widget.RelativeLayout[1]/android.widget.LinearLayout[1]/android.widget.ImageView[1]")
        # 如果当前页的所有6个选项都点完了，就进行向上滑动
        if (count != 0 and count % 6 == 0):
            time.sleep(3)
            # 向下滑
            swipeUp(driver, 1000, 1)
            time.sleep(3)
            small_count = 1
            count += 1
            continue
        # 没点完当前页面的6个选项，就判定是否是vip选项
        else:
            # 如果是vip用户则跳过
            if (ele):
                small_count += 1
                count += 1
                continue
            # 如果不是vip就点进去，然后向上滑动
            else:
                # 点击
                driver.find_element_by_xpath(
                    f'//*[@resource-id="lnwlztb.study:id/list"]/android.widget.RelativeLayout[{small_count}]').click()
                # 滑动
                time.sleep(3)
                swipeUp(driver, 1000, 10)
                time.sleep(3)
                # 点击返回到列表页面
                driver.find_element_by_id("lnwlztb.study:id/btn_back").click()
                small_count += 1
                count += 1
                continue

        # 此文章仅对会员用户开放，您可在“我的”模块办理会员服务。
        # vip = ("xpath", "//*[contains(@text,'此文章仅对会员用户开放，您可在“我的”模块办理会员服务。')]")
        # 打印toast的文案
        # print(len(vip))
        # if (len(vip) > 1):
        #     small_count += 1
        #     count += 1
        #     print(
        #         "=========================================================================================================")
        #     continue
        # else:
        #     time.sleep(3)
        #     # 向下滑
        #     swipeUp(driver, 1000, 10)
        #     time.sleep(3)
        # if (count % 6 == 0 and count != 0):
        #     swipeUp(driver, 3000, 1)
        #     small_count = 0
        # count += 1
        # small_count += 1
    # else:
    #     break
    # if(count_i == 7):
    #     count_i = 0
    #     pull_count = 4
    #     goto.begin

    # time.sleep(3)
    # # 向下滑
    # swipeUp(driver, 1000, 10)
    # time.sleep(3)
    # # 点击2次回退
    # driver.find_element_by_id("lnwlztb.study:id/btn_back").click()
    # time.sleep(10)
    # driver.find_element_by_id("lnwlztb.study:id/btn_back").click()
    # print(f"第{i + 1}次循环")

# rolling()
